package cn.tedu.ivos.repair.service.impl;

import cn.tedu.ivos.repair.mapper.RepairMapper;
import cn.tedu.ivos.repair.pojo.dto.RepairQuery;
import cn.tedu.ivos.repair.pojo.dto.RepairSaveParam;
import cn.tedu.ivos.repair.pojo.entity.Repair;
import cn.tedu.ivos.repair.pojo.vo.RepairVO;
import cn.tedu.ivos.repair.service.RepairService;
import cn.tedu.ivos.vehicle.pojo.dto.VehicleSaveParam;
import cn.tedu.ivos.vehicle.service.VehicleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class RepairServiceImpl implements RepairService {

    @Autowired
    RepairMapper repairMapper;
    @Autowired
    VehicleService vehicleService;

    @Override
    public List<RepairVO> select(RepairQuery repairQuery) {
        log.debug("查询业务，参数{}", repairQuery);
        List<RepairVO> list=repairMapper.select(repairQuery);
        log.debug("参数{}",list);
        return list;
    }

    @Override
    public void save(RepairSaveParam repairSaveParam) {
        log.debug("保存业务，参数{}", repairSaveParam);
        VehicleSaveParam vehicleSaveParam=new VehicleSaveParam();
        Repair repair=new Repair();
        BeanUtils.copyProperties(repairSaveParam,repair);
        vehicleSaveParam.setId(repair.getVehicleId());
        if (repair.getId()==null){ //新增业务
            repair.setCreateTime(new Date());
            repairMapper.insert(repair);


            vehicleSaveParam.setStatus("3");


        }else {
            repair.setUpdateTime(new Date());
            repairMapper.update(repair);
            if (repair.getStatus().equals("0")){
                vehicleSaveParam.setStatus("1");

            }
        }
        vehicleService.saveVehicle(vehicleSaveParam);

    }

    @Override
    public void deleteRepair(Long id) {
        log.debug("删除业务，参数{}",id);
        repairMapper.delete(id);

    }
}
